// 获取URL中的商品ID
function getGoodsId() {
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get('id');
}

// 加载商品详情
async function loadGoodsDetail() {
    try {
        const goodsId = getGoodsId();
        if (!goodsId) {
            alert('商品ID不存在！');
            return;
        }

        const response = await axios.get('/goods/item', {
            params: { id: goodsId }
        });

        if (response.data.code === 1) {
            renderGoodsDetail(response.data.info);
        } else {
            alert('获取商品详情失败：' + response.data.message);
        }
    } catch (error) {
        console.error('加载商品详情失败:', error);
        alert('加载商品详情失败，请稍后重试');
    }
}

// 渲染商品详情
function renderGoodsDetail(goods) {
    // 渲染基本信息
    const detailContent = document.getElementById('goodsDetail');
    const currentPrice = parseFloat(goods.current_price).toFixed(2);
    const originalPrice = goods.price ? parseFloat(goods.price).toFixed(2) : null;
    const discount = goods.sale_type ? goods.sale_type : '';

    detailContent.innerHTML = `
        <div class="goods-image-container">
            <img src="${goods.img_big_logo}" alt="${goods.title}" class="goods-image">
        </div>
        <div class="goods-info">
            <h1 class="goods-title">${goods.title}</h1>
            ${goods.category ? `<div class="goods-subtitle">品类：${goods.category}</div>` : ''}
            
            <div class="price-info">
                <div class="price-row">
                    <span class="price-label">价格</span>
                    <span class="current-price">${currentPrice}</span>
                    ${originalPrice ? `<span class="original-price">${originalPrice}</span>` : ''}
                    ${discount ? `<span class="discount">${discount}</span>` : ''}
                </div>
                <div class="price-row">
                    <span class="price-label">库存</span>
                    <span>${goods.goods_number}件</span>
                </div>
            </div>
        </div>
    `;

    // 渲染详细介绍
    const detailIntroduce = document.getElementById('goodsIntroduce');
    detailIntroduce.querySelector('.detail-content').innerHTML = goods.goods_introduce || '暂无商品详细介绍';
}

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
    loadGoodsDetail();
}); 